package com.fyzk.activity.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.base.BaseFragment;
import com.fyzk.activity.news.NewsOldActivity;
import com.fyzk.activity.special.CoinDetailActivity;
import com.fyzk.activity.special.ScoreLiveStreamActivity;
import com.fyzk.activity.special.VirtualHallActivity;
import com.fyzk.activity.special.VirtualMoneyExchangeActivity;
import com.fyzk.activity.trend.DLTTrendActivity;
import com.fyzk.activity.trend.SSQTrendActivity;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.bean.FindJifenBean;
import com.fyzk.bean.FindVirtualMoneyBean;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;
import com.tyjc.unique.DLTUniqueActivity;
import com.tyjc.unique.SSQUniqueActivity;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import http.Common.CommandUtil;
import http.DataUtil.md5.MD5;
import http.StringResultBean;

/**
 * 特色功能
 */
@ContentView(R.layout.fragment_special)
public class SpecialFragment extends BaseFragment {

    private final String TAG = this.getClass().getSimpleName();

    private TopTitleView topTitle;

    @ViewInject(R.id.coin_detail)
    private Button coin_detail;

    @ViewInject(R.id.vitualhall_btn)
    private Button vitualhall_btn;

    @ViewInject(R.id.coins)
    private TextView coins;

    @ViewInject(R.id.vitual_money)
    private TextView vitual_money;

    @ViewInject(R.id.chend)
    private Button chend;

    @ViewInject(R.id.cold_hot)
    private Button cold_hot;

    @ViewInject(R.id.news)
    private Button hall_news;

    @ViewInject(R.id.filter)
    private Button filter;

    @ViewInject(R.id.month_pay)
    private Button month_pay;

    @ViewInject(R.id.ssq_prizeverify)
    private Button ssq_prizeverify;

    @ViewInject(R.id.cold_hot_dlt)
    private Button cold_hot_dlt;

    @ViewInject(R.id.month_pay_dlt)
    private Button month_pay_dlt;

    @ViewInject(R.id.prizeverify_dlt)
    private Button prizeverify_dlt;

    @ViewInject(R.id.chend_dlt)
    private Button chend_dlt;

    private long jiFen = 0;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);//xutil注解;
    }


    protected void request() {
        if(MySharedPreferences.getInstance().getLoginStatus()){
            findUserJifen();
            findVirtualMoney();
        }
    }


    /**
     * 查询积分
     */
    private void findUserJifen() {
        JSONObject params = new JSONObject();
        try {
            params.put("tranid", MD5.toMd5((System.currentTimeMillis() + "").getBytes()));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpPostJson(CommandUtil.FINDUSERJIFEN, Commands.FINDUSERJIFEN, params, false);
    }

    /**
     * 查询虚拟钱
     */
    private void findVirtualMoney() {
        JSONObject params = new JSONObject();
        httpPostJson(CommandUtil.FINDUSERVIRTUALACCOUNT, Commands.FINDUSERVIRTUALACCOUNT, params, false);
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
//        super.onSuccess(requestCode,resultBean,result);

        switch (requestCode) {
            case CommandUtil.FINDUSERJIFEN:

                FindJifenBean bean = JSON.parseObject(resultBean.getData(), FindJifenBean.class);
                try {
                    jiFen = Long.parseLong(bean.getBalance());
                } catch (Exception e) {

                }
                coins.setText("积分：" + jiFen);
                break;
            case CommandUtil.FINDUSERVIRTUALACCOUNT:
                FindVirtualMoneyBean bean1 = JSON.parseObject(resultBean.getData(), FindVirtualMoneyBean.class);
                vitual_money.setText("虚拟币: "+bean1.getBalance()/100);
                break;
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        initData();

    }

    private void net() {
//        topTitle.getRightImgCom().startAnimation(anim);
//        request();
    }

    private void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        net();
    }

    protected void init() {

//        mLVi_prizenums.item(listener);
    }


    public Context getContext() {
        return getActivity();
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onCenterClick() {

    }

    @Override
    public void onRightClick() {
        net();
    }


    @Override
    public void onRight2Click() {

    }


    @Event(type = View.OnClickListener.class, value = {R.id.chend, R.id.cold_hot, R.id.filter, R.id.month_pay,
            R.id.ssq_prizeverify, R.id.cold_hot_dlt,R.id.news, R.id.month_pay_dlt,
            R.id.prizeverify_dlt, R.id.chend_dlt,R.id.coin_detail,R.id.vitualhall_btn, R.id.coin_spend,R.id.jc_zhibo})
    private void onClickEvent(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.chend:
//                intent = new Intent(getContext(), SSQUniqueActivity.class);
//                intent.putExtra(IntentParamUtils.FUNCTION, SSQUniqueActivity.TREND);
//                startActivity(intent);
                intent = new Intent(getContext(), SSQTrendActivity.class);
                intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils.SSQ_LOTTERY_ID_INT);
                startActivity(intent);
                break;
            case R.id.cold_hot:
                intent = new Intent(getContext(), SSQUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, SSQUniqueActivity.COLDHOT);
                startActivity(intent);
                break;
            case R.id.filter:
                intent = new Intent(getContext(), SSQUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, SSQUniqueActivity.FILTER);
                startActivity(intent);
                break;
            case R.id.month_pay:
                intent = new Intent(getContext(), SSQUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, SSQUniqueActivity.LUCKYNUM);
                startActivity(intent);
                break;
            case R.id.ssq_prizeverify:
                intent = new Intent(getContext(), SSQUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, SSQUniqueActivity.PRIZEVERFY);
                startActivity(intent);
                break;
            case R.id.chend_dlt:
                intent = new Intent(getContext(), DLTTrendActivity.class);
                intent.putExtra(IntentParamUtils.LOTTERYID, LotteryIDUtils.DLT_LOTTERY_ID_INT);
                startActivity(intent);
                break;
            case R.id.news:
                intent = new Intent(getContext(), NewsOldActivity.class);
                startActivity(intent);
                break;
            case R.id.cold_hot_dlt:
                intent = new Intent(getContext(), DLTUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, DLTUniqueActivity.COLDHOT);
                startActivity(intent);

                break;
            case R.id.month_pay_dlt:
                intent = new Intent(getContext(), DLTUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, DLTUniqueActivity.LUCKYNUM);
                startActivity(intent);
                break;
            case R.id.prizeverify_dlt:
                intent = new Intent(getContext(), DLTUniqueActivity.class);
                intent.putExtra(IntentParamUtils.FUNCTION, DLTUniqueActivity.PRIZEVERFY);
                startActivity(intent);
                break;
            case R.id.coin_spend:   // 消费积分
                if(MySharedPreferences.getInstance().getLoginStatus()){
                    intent = new Intent(getContext(), VirtualMoneyExchangeActivity.class);
                    intent.putExtra("jifen", jiFen);
                    startActivity(intent);
                }else{
                    AppLotteryInfos.clear();
                    ViewCallBack.getInstance().countDownObjs.clear();
                    ActivityTaskManager.getInstance().closeAllActivity();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.coin_detail:  // 积分详情
                if(MySharedPreferences.getInstance().getLoginStatus()) {
                    intent = new Intent(getContext(), CoinDetailActivity.class);
                    startActivity(intent);
                }else{
                    AppLotteryInfos.clear();
                    ViewCallBack.getInstance().countDownObjs.clear();
                    ActivityTaskManager.getInstance().closeAllActivity();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.vitualhall_btn:   // 虚拟大厅
                if(MySharedPreferences.getInstance().getLoginStatus()) {
                    intent = new Intent(getContext(), VirtualHallActivity.class);
                    startActivity(intent);
                }else{
                    AppLotteryInfos.clear();
                    ViewCallBack.getInstance().countDownObjs.clear();
                    ActivityTaskManager.getInstance().closeAllActivity();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.jc_zhibo:
                intent = new Intent(getContext(), ScoreLiveStreamActivity.class);
                startActivity(intent);
                break;
        }

    }

}
